using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;
using CMS.Web;

namespace CMS.Web.ListManager
{
    public partial class AddLichTiep : BaseUserControl, IAddLichTiepView
    {
        private AddLichTiepPresenter presenter;

        public AddLichTiep()
        {
            this.presenter = new AddLichTiepPresenter(this, NHibernateDaoFactory.Instance.GetLichTiepDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // For Ajax
            this.RaiseClientEvent();
           
            this.btnOK.Attributes.Add("onclick", "return CheckInput(0);");
            this.btnOKC.Attributes.Add("onclick", "return CheckInput(1);");
            this.chkTrangThai.Checked = true;
            this.txtNgayBanHanh.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddVanBanPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {   
            presenter.AddObj(Page.IsValid);
            string strFolderPath = "lichtiep";
            string strFile = CMS.Web.Files.Instance.UpLoadFile(this.txtFileUpload.PostedFile, strFolderPath);
            this.btnCancel_Click(this, null);
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            string strFolderPath = "lichtiep";
            string strFile = CMS.Web.Files.Instance.UpLoadFile(this.txtFileUpload.PostedFile, strFolderPath);           
            presenter.AddObj(Page.IsValid);
            this.txtCQBH.Text = "";
            this.txtCQBH.Focus();            
            this.txtSoHieu.Text = "";
            this.txtTrichDan.Text = "";
            this.txtNgayBanHanh.Text = "";
            this.txtBody.Value = "";
            //this.chkTrangThai.Checked = false; 
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }

        #region IAddLichTiepView Members


        public LichTiep LichTiepToAdd
        {
            get 
            {
                LichTiep obj = new LichTiep();                
                obj.KyHieu = this.txtSoHieu.Text.Trim();
                obj.TrichDan = this.txtTrichDan.Text.Trim();
                obj.TenFileDinhKem = CMS.Web.Files.Instance.StandardName("lichtiep", this.txtFileUpload.PostedFile);
                if (!txtNgayBanHanh.Text.Trim().Equals(""))
                {
                    string[] temp = txtNgayBanHanh.Text.Trim().Split(new Char[] { '/' });
                    obj.NgayKy = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                }
                else
                {
                    obj.NgayKy = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, 0);
                }
                obj.CQBH = this.txtCQBH.Text.Trim();
                obj.NoiDung = txtBody.Value;
                obj.TrangThai = true;
                //obj.TrangThai = this.chkTrangThai.Checked;
                return obj;
            }
        }

        #endregion
    }
}